<template>
  <div id="upload_demo1">
    <h3>添加新用户</h3>
    <el-form :model="userForm" :rules="rules" ref="userFormRef" label-width="100px">
      <el-form-item label="用户帐号" prop="loginId">
        <el-input v-model="userForm.loginId"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input type="password" v-model="userForm.password"></el-input>
      </el-form-item>
      <el-form-item label="用户头像">
        <el-upload
            name="photo"
            class="avatar-uploader"
            action="http://localhost:9999/sysusers/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('userFormRef')">添加</el-button>
        <el-button @click="resetForm('userFormRef')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export  default {
  data() {
    return {
      imageUrl: '',
      userForm: {
        loginId: 'admin',
        password: '123456'
      },
      rules: {
        loginId: [
          { required: true, message: '请输入登录用户', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res} = await this.$http.post('/sysusers/add', this.userForm)
          console.log('res', res)
          if(res.data === 'ok') {
            alert('ok')
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 头像成功上传的回调
    handleAvatarSuccess(res, file) {
      console.log('res', res)
      console.log('file', file)
      this.imageUrl = res;
      this.userForm.photoPath = this.imageUrl;
    },
    // 上传之前校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
